package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainManager;
import com.qf.service.FilterChainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/10/19 17:29
 * zer链管理者实现类
 */
@Service
@Slf4j
public class FilterChainManagerImpl implements FilterChainManager {
    private Map<String, FilterChainService> filterMap;
    @Autowired
    private CacheFeign cacheFeign;

    @Override
    public void executeFilter(StandardSubmit standardSubmit) {
//     获取全部策略对象，这里是注入spring容器中的所有对象@Autowired
//     1.调用cache模块获取redis中过滤器的执行顺序
        Map<String, String> clientMap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_CLIENT + standardSubmit.getClientID());
//     例如: "black,router,fee,phase,limit,dirtyWord"
//     2.根据逗号分割，,获取过滤器执行顺序数组
        String filters = clientMap.get("filters");
        String[] filterArray = filters.split(",");
        if (filterArray != null) {
            //     3.遍历过滤器执行顺序数组
            for (String filterName : filterArray) {
//     4.获取执行的过滤器实现类对象, FilterChainService(blackserviceimpl) chain = filterMap.get(" black")
                FilterChainService filterChainService = filterMap.get(filterName);

//     5.执行具体策略业务方法, chain.strategy(StandardSubmit submit);
                boolean flag = filterChainService.strategy(standardSubmit);
//     6.判断执行结果,如果返回为false停止执行,返回为true继续执行
                if (!flag) {
                    log.info("[策略模块 - 责任    者管理者实现类]策略执行失败,导致中断");
                    break;
                }
            }
        }


    }
}
